package arithmetic.thread;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author jiangfeng 2019/7/16 00:26
 * 阻塞队列之同步队里 ， 生产一个消费一个
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        SynchronousQueue<Object> synchronousQueue = new SynchronousQueue<>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"生产一个");
                synchronousQueue.put("1");
                System.out.println(Thread.currentThread().getName()+"生产一个");
                synchronousQueue.put("1");
                System.out.println(Thread.currentThread().getName()+"生产一个");
                synchronousQueue.put("1");

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()->{

            try {
                System.out.println( Thread.currentThread().getName()+"消费一个 :" +synchronousQueue.take());
                TimeUnit.SECONDS.sleep(2);

                System.out.println( Thread.currentThread().getName()+"消费一个 :" +synchronousQueue.take());
                TimeUnit.SECONDS.sleep(2);

                System.out.println( Thread.currentThread().getName()+"消费一个 :" +synchronousQueue.take());
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }

}
